import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;

public class isAvailable {
    private static final String BASE_URL = "http://121.36.106.217:9005/";
    private static final String MCHID = "20170100";
    private static final String APPID = "20221120242";
    private static final OkHttpClient client = new OkHttpClient();

    // 检查支付接口是否可用并返回token
    public static String isAvailable() throws IOException {
        String url = BASE_URL + "wechat/isRunning"; // 微信的URL
//        String url = BASE_URL + "/alipay/isRunning"; // 支付宝的URL
        // 付款接口根据支付方式（微信/支付宝）选择不同的URL
        // 此代码以微信为例，支付宝类似处理

        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                System.out.println("请求失败，响应代码:" + response.code());
                return null;
            }
            String responseData = response.body().string();
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonResponse = objectMapper.readTree(responseData);
            if (jsonResponse.has("code") && jsonResponse.get("code").asInt() == 200) {
                JsonNode data = jsonResponse.get("data");
                if (data != null && data.has("token")) {
                    String token = data.get("token").asText();
                    System.out.println("获取的token：" + token);
                    return token;
                }
            }
        }
        return null;
    }
}
